package com.note.chh.notebook.UI.enjoy.singerlist;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Adapter.SingerListAdapter;
import com.note.chh.notebook.Adapter.SongListAdapter;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.RankListModel;
import com.note.chh.notebook.Model.SongListModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.UI.enjoy.PlayMusicActivity;
import com.note.chh.notebook.UI.enjoy.rank.RankListActivity;
import com.note.chh.notebook.Uitls.PopupUtils;
import com.note.chh.notebook.Uitls.gson.GsonImpl;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//歌曲列表
public class SongListActivity extends BaseActivity {


    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mRecycler)
    RecyclerView mRecycler;
    private ImageView mImage_singer;
    private TextView mText_info,mText_more;
    private View inflate;
    private BaseQuickAdapter songListAdapter;

    private int Singerid = 0;
    private int pagerIndex = 1;
    private int pagerSize = 0;
    private String intro = "";


    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int bindLayout(){
        return R.layout.activity_song_list;
    }

    @Override
    public void initView() {
        QMUIStatusBar(SongListActivity.this);

        Bundle bundle = getIntent().getExtras();
        Singerid = bundle.getInt("Singerid");
        String singername = bundle.getString("Singername");
        topbar.setTitle(singername);

        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        songListAdapter = new SongListAdapter(R.layout.item_rank_list);
        songListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecycler.setAdapter(songListAdapter);


        inflate = LayoutInflater.from(this).inflate(R.layout.header_song_list, null);
        mImage_singer = inflate.findViewById(R.id.mImage_singer);
        mText_info = inflate.findViewById(R.id.mText_info);
        mText_more = inflate.findViewById(R.id.mText_more);

        mText_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
            }
        });
    }
    //pop展示明星介绍详情
    private QMUIPopup qmuiPopup;
    private void showPop(){
        if(qmuiPopup==null){
           qmuiPopup = PopupUtils.normalPopup(this, intro, getResources().getColor(R.color.app_color_blue));
         }
//        qmuiPopup.setPositionOffsetYWhenTop(50);
        qmuiPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
        qmuiPopup.setPreferredDirection(QMUIPopup.DIRECTION_BOTTOM);
        qmuiPopup.show(mText_more);
    }


    @Override
    public void initData() {
        getSingerInfo();
    }

    @Override
    public void initListener() {
        super.initListener();
        songListAdapter.setEnableLoadMore(true);

        songListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                pagerIndex ++;
                getSingerInfo();
            }
        },mRecycler);


        songListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!NetworkUtils.isAvailableByPing()) {//判断网络是否可用
                    showToast(SongListActivity.this, Constant.NETWORKNOUSER);
                } else if (NetworkUtils.isMobileData()) {//判断是否是移动数据  是
                    showMessageDialog(position);
                }else{//否
                    goJump(position);
                }
            }
        });

    }
    //跳转处理
    private void goJump(int position){
        SongListModel.SongsBean.ListBean listBean = (SongListModel.SongsBean.ListBean) songListAdapter.getData().get(position);
        String hash = listBean.getHash();
        Bundle bundle = new Bundle();
        bundle.putString("hash",hash);
        bundle.putBoolean("netMusic",true);
        bundle.putInt("position",position);
        bundle.putInt("songType",2);
        bundle.putParcelableArrayList("songList", (ArrayList<SongListModel.SongsBean.ListBean>) songListAdapter.getData());
        startActivity(PlayMusicActivity.class,bundle);
    }

    //提示网络状态
    private void showMessageDialog(final int position) {
        new QMUIDialog.MessageDialogBuilder(SongListActivity.this)
                .setTitle("提示")
                .setMessage("您当前网络不是wifi，是否继续播放音乐")
                .addAction("不了，心疼流量", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("继续，我流量多", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        goJump(position);
                        dialog.dismiss();
                    }
                })
                .show();
    }

    //获取歌手的歌曲
    private void getSingerInfo() {
        OkGo.<String>get("http://m.kugou.com/singer/info/" + Singerid + "&page="+pagerIndex+"&json=true")
            .tag(this)
            .execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
//                    UserUtil.dLog("---response---"+response.body());

                    SongListModel songListModel = GsonImpl.get().toObject(response.body(), SongListModel.class);
                    List<SongListModel.SongsBean.ListBean> list = songListModel.getSongs().getList();

                    if (pagerIndex==1) {
                        SongListModel.InfoBean info = songListModel.getInfo();
                        intro = info.getIntro();
                        String imgurl = info.getImgurl().replace("{size}","240");
                        Glide.with(SongListActivity.this).load(imgurl).into(mImage_singer);
                        mText_info.setText(intro);
                        songListAdapter.addHeaderView(inflate);
                        songListAdapter.setNewData(list);
                        pagerSize = songListModel.getSongs().getPagesize();
                    }else{
                        if (list.size()<pagerSize) {
                            songListAdapter.loadMoreEnd();
                        }else{
                            songListAdapter.loadMoreComplete();
                        }
                        songListAdapter.addData(list);
                    }
                }
            });
    }
}
